﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TLSharp;

public partial class SettingsProcess : System.Web.UI.Page
{
    public string AppGUID = System.Configuration.ConfigurationSettings.AppSettings["AppGuid"];
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Text.StringBuilder ResponseText = new System.Text.StringBuilder();

        string Username = Request.Form["Username"];
        string Password = Request.Form["Password"];
        string Method = (Request["Method"] != null ? Request["Method"] : "");

        //test to see if the account is valid
        if (Method == "ForgetMe")
        {
            Response.Cookies["TLUsername"].Value = "";
            Response.Cookies["TLPassword"].Value = "";
            ResponseText.Append("<ul><li>Settings Saved</li><li><a href='default.aspx' target=_self>Back Home</a></li></ul>");

        }
        else
        {
            bool bValid = false;

            GetUserProfileResponse Validate = TLSharp.TouchLeagueService.GetUserProfile(Username, Password, AppGUID);

            if (Validate.ErrorCode < 0)
            {

                bValid = true;
            }

            if (bValid)
            {

                if (Response.Cookies.AllKeys.Contains("TLUsername"))
                {
                    Response.Cookies["TLUsername"].Value = Username;
                    Response.Cookies["TLUsername"].Expires = System.DateTime.Now.AddMonths(1200);
                    Response.Cookies["TLPassword"].Value = Password;
                    Response.Cookies["TLPassword"].Expires = System.DateTime.Now.AddMonths(1200);

                }
                else
                {
                    Response.Cookies.Add(new HttpCookie("TLUsername", Username));
                    Response.Cookies["TLUsername"].Expires = System.DateTime.Now.AddMonths(1200);
                    Response.Cookies.Add(new HttpCookie("TLPassword", Password));
                    Response.Cookies["TLPassword"].Expires = System.DateTime.Now.AddMonths(1200);

                }
                ResponseText.Append("<ul><li>Settings Saved</li><li><a href='default.aspx' target=_self>Back Home</a></li></ul>");

            }
            else
            {
                ResponseText.Append("<ul><li>Invalid credentials</li><li><a href='default.aspx' target=_self>Back Home</a></li></ul>");

            }
        }
        Response.Write(ResponseText.ToString());
        Response.End();
        
    }
}
